package cn.gcjxsc.service.task;

import cn.gcjxsc.consts.OrderUtil;
import cn.gcjxsc.entity.mall.WaitForDoneOrderInfo;
import cn.gcjxsc.queue.ReadWriteDoubleQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author pumbaa
 */
@Service
public class AutoOrderService {
    @Autowired
    private TaskOrderBusiService orderBusiService;

    private static ReadWriteDoubleQueue<WaitForDoneOrderInfo> queues = new ReadWriteDoubleQueue<>(50000);

    public void offer(WaitForDoneOrderInfo data) {
        queues.offer(data);
    }

    public WaitForDoneOrderInfo poll() {
        return queues.poll();
    }

    public int getQueueSize() {
        return queues.size();
    }

    public List<WaitForDoneOrderInfo> queryUnprocessOrderInfoList(int max) {
        List<WaitForDoneOrderInfo> list = orderBusiService.queryUnprocessOrderInfoList(max);
        if (list.size() > 0) {
            list.forEach(d -> {
                d.setProcessStatus(OrderUtil.QueueStatus.PROCESSING.getStatus());
                offer(d);
            });
            list = orderBusiService.save(list);
        }
        return list;
    }

    public String doOrderNotify(WaitForDoneOrderInfo info) {
        return orderBusiService.notifyProcessBusiness(info);
    }
}
